package com.android.tio.tiocloud.tio.client.handle;

import com.android.tio.client.intf.TioClientHandler;
import com.android.tio.core.ChannelContext;
import com.android.tio.core.intf.Packet;
import com.android.tio.tiocloud.tio.common.handle.AbstractAioHandler;
import com.android.tio.tiocloud.tio.common.packet.CloudPacket;

import org.greenrobot.eventbus.EventBus;

public class MyClientTioHandler extends AbstractAioHandler implements TioClientHandler {
    /**
     * 心跳包
     */
    private static final CloudPacket HEARTBEAT_PACKET = new CloudPacket();

    @Override
    public void handler(Packet packet, ChannelContext channelContext) throws Exception {
        CloudPacket helloPacket = (CloudPacket) packet;
        byte[] body = helloPacket.getBody();
        if (body != null) {
            String str = new String(body, CloudPacket.CHAR_SET);
            EventBus.getDefault().post(str);
        }

    }

    @Override
    public Packet heartbeatPacket(ChannelContext channelContext) {
        return HEARTBEAT_PACKET;
    }
}
